Date.prototype.Format = function(fmt)   {
  var o = {   
    "M+" : this.getMonth()+1,                 // 月份
    "d+" : this.getDate(),                    // 日
    "h+" : this.getHours(),                   // 小时
    "m+" : this.getMinutes(),                 // 分
    "s+" : this.getSeconds(),                 // 秒
    "q+" : Math.floor((this.getMonth()+3)/3), // 季度
    "S"  : this.getMilliseconds()             // 毫秒
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}
function toDate(date){
	var t = Date.parse(date);  
    if (!isNaN(t)) {  
        return new Date(Date.parse(date.replace(/-/g, "/")));  
    } else {  
        return new Date();  
    }  
}
app.controller(
	"fatherCtrl",
	function($scope, request) {
		$scope.logout = function() {
			request.getData("get", "user/outLogin", null).then(
					function(data) {
						window.location.href = "/login.html";
					});
		}
		$scope.sh = true;
		$scope.sh1 = true;
		$scope.f = true;
		$scope.f1 = true;
		$scope.f2 = false;
		$scope.f3 = false;
		$scope.show = function() {
			$scope.f = true;
			$scope.f1 = true;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = false;
			$('#sidebar li.submenu ul').slideUp();
			$(".submenu").removeClass("open");
		}
		$scope.show1 = function() {
			$scope.f = false;
			$scope.f1 = false;
			$scope.f11 = true;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = false;
		}
		$scope.show2 = function() {
			$scope.f = false;
			$scope.f1 = false;
			$scope.f11 = false;
			$scope.f12 = true;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = false;
		}
		$scope.show3 = function() {
			$scope.f = false;
			$scope.f1 = false;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = true;
			$scope.f2 = false;
			$scope.f3 = false;
		}
		$scope.show4 = function() {
			$scope.f = false;
			$scope.f1 = true;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = true;
			$scope.f3 = false;
			$('#sidebar li.submenu ul').slideUp();
			$(".submenu").removeClass("open");
		}
		$scope.show5 = function() {
			$scope.f = false;
			$scope.f1 = true;
			$scope.f11 = false;
			$scope.f12 = false;
			$scope.f13 = false;
			$scope.f2 = false;
			$scope.f3 = true;
			$('#sidebar li.submenu ul').slideUp();
			$(".submenu").removeClass("open");
		}
	})
.controller("myselfController",
	function($scope, request) {
		function users(){
			request.getData("get", "user/myself", {}).then(function(data) {
				var datas=data.data;
				if (data.code == 1) {
					for(var i=0;i<datas.length;++i){
						datas[i].birth=toDate(datas[i].birth);
					}
					$scope.data = datas[0];
				}
			});
		}
		users();
		$scope.upd = function() {
			var sex=$("select").val();
			if($scope.data.username==null||$scope.data.username==""){
				jin("请填写员工工号");
			}else if($scope.data.name==null||$scope.data.name==""){
				jin("请填写员工姓名");
			}else if(sex==null||sex==""){
				jin("请填写员工性别");
			}else if($scope.data.birth==null||$scope.data.birth==""){
				jin("请填写员工出生日期");
			}else{
				request.getData("post", "user/updateUser", {
					name:$scope.data.name,
					birth:new Date($scope.data.birth).Format("yyyy-MM-dd"),
					sex:sex,
					address:$scope.data.address
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
						$scope.disadd();
						users();
					}else{
						tip(data.msg);
					}
				});
			}
		};
		$scope.disupdate = function(data) {
			data.s = false;
			data.u = false;
		};
		$scope.toDeletet=function(data){
			$scope.dId=data.id;
		}
		$scope.deletet = function() {
			request.getData("post", "user/deleteUser", {
				userId:$scope.dId
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
		$scope.toUpdate=function(data){
			$scope.uId=data.id;
		}
		$scope.updatePwd = function() {
			request.getData("post", "user/updatePwd", {
				userId:$scope.uId,
				pwd:$scope.uPwd
			}).then(function(data) {
				if (data.code == 1) {
					suc("成功");
					$scope.disadd();
					users();
				}else{
					tip(data.msg);
				}
			});
		};
	})
	.controller("passwordController",
			function($scope, request) {
		$scope.upd = function() {
			if($scope.pwd1==$scope.pwd2){
				jin("新密码与原密码相同");
			}else if ($scope.pwd2!=$scope.pwd3){
				jin("密码两次输入不相同");
			}else{
				request.getData("post", "user/updateMyPwd", {
					oldPwd:$scope.pwd1,
					pwd:$scope.pwd2
				}).then(function(data) {
					if (data.code == 1) {
						suc("成功");
					}else{
						tip(data.msg);
					}
				});
			}
		};
	})